package fun.coding.leetcode;

import java.util.ArrayList;
import java.util.List;

public class ReverseNodesInKGroup {

	public static void main(String[] args) {

	}
	
	public ListNode reverseKGroup(ListNode head, int k) {
		if (head == null || head.next == null || k == 0 || k == 1) return head;
		
		int size = getListSize(head);
		int c = size / k;
		List<ListNode> l = new ArrayList<ListNode>();
		ListNode dummy = new ListNode(-1);
		ListNode cur = dummy;
		
		for (int i = 0; i < c; i++) {
			ListNode newHead = cur.next;
			cur.next = null;
			l.add(newHead);
			
			int j = k - 1;
			while (j > 0) {
				cur = cur.next;
				j--;
			}
		}
		
		List<ListNode> reversed = new ArrayList<ListNode>();
		for (ListNode n : l) {
			reversed.add(reverseList(n));
		}
		
		// TODO: no, not a good idea
		for (int i = 1; i < reversed.size(); i++) {
			
		}
		
		return null;
	}
	
	public ListNode reverseList(ListNode head) {
		if (head == null || head.next == null) return head;
		
		ListNode dummy = new ListNode(-1);
		
		ListNode cur = head;
		
		while (cur != null) {
			ListNode next = cur.next;
			cur.next = dummy.next;
			dummy.next = cur;
			cur = next;
		}
		return dummy.next;
	}
	
	public int getListSize(ListNode head) {
		int count = 0;
		while (head != null) {
			count++;
			head = head.next;
		}
		return count;
	}

}
